// pages/login/login.js
const request = require('../../utils/request'); // 引入封装好的请求工具
const userjs = require('../../utils/user'); // 引入封装好的请求工具

Page({
  data: {
    active: 0, // 0 表示账号登录，1 表示验证码登录
    username: 'admin',
    password: 'admin',
    phone: '15888888888',
    code: '',
    uuid: '',
    rememberMe: false,
    zhengce: false,
    loading: false,
    captchaEnabled: true,
    codeUrl: '',
    loginImage: '/assets/images/登录左侧图片.png',
    title: '星源组智慧社区',
    subtitle: '物业信息管理平台',
    circles: Array.from({ length: 20 }, (_, index) => index + 1) // 生成1到20的数组
  },
  onLoad() {
    this.setData({
      circles: Array.from({ length: 20 }, (_, index) => index + 1)
    });
    this.getCode(); // 获取验证码图片
  },
  // 切换登录方式
  onChange(event) {
    this.setData({
      active: event.detail
    });
  },
  // 获取验证码图片
  getCode() {
    const url = '/captchaImage';
    request.get(url)
      .then(response => {
        console.log(response);
        this.setData({
          codeUrl: 'data:image/gif;base64,' + response.img,
          uuid: response.uuid
        });
      })
      .catch(error => {
        console.error('获取验证码失败:', error);
      });
  },
  // 获取短信验证码
  getSmsCode() {
    const url = '/yanCode';
    request.get(url)
      .then(response => {
        console.log(response);
        this.setData({
          code: response.data.yanCode,
          uuid: response.data.uuid
        });
        wx.showToast({
          title: '验证码已发送',
          icon: 'success'
        });
      })
      .catch(error => {
        console.error('发送验证码失败:', error);
      });
  },
  // 登录处理
  login() {
    if (!this.data.zhengce) {
      wx.showToast({
        title: '请先同意隐私政策',
        icon: 'none'
      });
      return;
    }
    this.setData({
      loading: true
    });
    const url = '/login';
    const { username, password, uuid, code } = this.data;
    request.post(url, { username, password, uuid, code })
      .then(response => {
        console.log(response.token);
        if (response.code === 200) {
          // 存储 token 到本地存储
          wx.setStorageSync('token', response.token);
          userjs.getUserProfile().then(res=>{
            if (res.data.isStaff != 1) {
              wx.showToast({
                title: '你个用户，又不是员工，登录个damn',
                icon: 'none'
              });
              this.setData({
                loading: false
              });
              return;
            }else{
              // 存储 token 到本地存储
              
              // 存储全局变量
              wx.setStorageSync('userInfo', res)
              console.log('存储成功userInfo', wx.getStorageSync('userInfo'));
              wx.showToast({
                title: '登录成功',
                icon: 'success'
              });
              
              // 页面跳转或其他逻辑
              wx.switchTab({
                url: '/pages/wode/wode',
              });
            }
            
          })

        }else{
          console.error('登录失败:', error);
          this.setData({
            loading: false
          });
        }
      })
      .catch(error => {
        console.error('登录失败:', error);
        this.setData({
          loading: false
        });
      });
  },
  // 手机号登录处理
  phoneLogin() {
    if (!this.data.zhengce) {
      wx.showToast({
        title: '请先同意隐私政策',
        icon: 'none'
      });
      return;
    }
    this.setData({
      loading: true
    });
    const url = '/loginByPhone';
    const { phone, code, uuid } = this.data;
    request.post(url, { phone, code, uuid })
      .then(response => {
        console.log(response);
        if (response.code === 200) {
          // 存储 token 到本地存储
          wx.setStorageSync('token', response.token);
          //存储user的信息
          userjs.getUserProfile().then(res=>{
            if (res.data.isStaff != 1) {
              wx.showToast({
                title: '你个用户，又不是员工，登录个damn',
                icon: 'none'
              });
              this.setData({
                loading: false
              });
              return;
            }else{

              // 存储全局变量
              wx.setStorageSync('userInfo', res)
              console.log('存储成功userInfo', wx.getStorageSync('userInfo'));
              wx.showToast({
                title: '登录成功',
                icon: 'success'
              });
              
              // 页面跳转或其他逻辑
              wx.switchTab({
                url: '/pages/wode/wode',
              });
            }
            
          })

        }else{
          console.error('登录失败:', error);
          this.setData({
            loading: false
          });
        }
      })
      .catch(error => {
        console.error('登录失败:', error);
        this.setData({
          loading: false
        });
      });
  },
  // 更新用户名
  updateUsername(event) {
    this.setData({
      username: event.detail
    });
  },
  // 更新密码
  updatePassword(event) {
    this.setData({
      password: event.detail
    });
  },
  // 更新手机号
  updatePhone(event) {
    this.setData({
      phone: event.detail
    });
  },
  // 更新验证码
  updateCode(event) {
    this.setData({
      code: event.detail
    });
    console.log(event.detail);
  },
  // 更新记住密码状态
  updateRememberMe(event) {
    this.setData({
      rememberMe: event.detail
    });
  },
  // 更新隐私政策同意状态
  updateZhengce(event) {
    this.setData({
      zhengce: !this.data.zhengce
    });
  },
  // 跳转到忘记密码页面
  toForget() {
    wx.navigateTo({
      url: '/pages/resetPwd/resetPwd'
    });
  },
  toZhuCe(){
    wx.navigateTo({
      url: '/pages/register/register'
    });
  },
  // 跳转到隐私政策页面
  toPolicy() {
    wx.navigateTo({
      url: '/pages/policy/policy'
    });
  }
});